#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    vector<vector<int>> dp(k, vector<int>(2));
    int v;
    scanf("%d", &v);
    for (int i = 0; i < k; ++i)
    {
        dp[i][0] = -v;
        dp[i][1] = 0;
    }
    for (int i = 1; i < n; ++i)
    {
        scanf("%d", &v);
        dp[0][0] = max(dp[0][0], -v);
        dp[0][1] = max(dp[0][1], dp[0][0] + v);
        for (int j = 1; j < k; ++j)
        {
            dp[j][0] = max(dp[j - 1][1] - v, dp[j][0]);
            dp[j][1] = max(dp[j][1], dp[j][0] + v);
        }
    }
    cout << dp[k - 1][1] << endl;
    return 0;
}
